/**
 * 
 */
package utils;

import java.io.*;

/**
 * @author Yoni Seroussi
 * @author Roei Raviv
 *
 */
public class FileUtils {

    private static final String OUTPUT_FILE = "_ResultFile.csv";
    private static final String WIN_OUTPUT_FILE = "C:/temp/";
    private static final String UNIX_OUTPUT_FILE = "/tmp/";
    private static final String UNDERSCORE = "_";

    /**
     * @param filenameSepcific part of the filename can be emptyString but 
     *  CANNOT be null
     * @param strBufToSave data to write to the file
     * @param append true iff we want to ADD data to the file, false if we want 
     *  the file to be deleted before adding the data. 
     */
    public static void saveToFile(String filenameSepcific,
            StringBuffer strBufToSave, boolean append) {
        File reportFile = null;
        String sunDesktop = System.getProperty("sun.desktop");
        String fNameSuffix = UNDERSCORE + filenameSepcific + OUTPUT_FILE;
        if (sunDesktop != null && sunDesktop.equalsIgnoreCase("windows")) {
            reportFile = new File(WIN_OUTPUT_FILE + fNameSuffix);
        } else {
            reportFile = new File(UNIX_OUTPUT_FILE + fNameSuffix);
        }
        try {
            reportFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

        FileWriter fw = null;
        try {
            fw = new FileWriter(reportFile, append);
            fw.write(strBufToSave.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != fw) {
                try {
                    fw.close();
                } catch (IOException e) {
                    System.out.println("failed closing file writer ");
                    e.printStackTrace();
                }
            }
        }
    }
}